﻿using AdminStyem.Application.Command.FeedstockCommand;
using AdminStyem.Application.DTO;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.FeedstockHandler
{
    public class FeedstockShowCommandHandler : IRequestHandler<FeedstockShowCommand, APIResult<List<FeedbackDTO>>>
    {
        IBaseRespository<Feedstock> BaseEeedstork;

        public FeedstockShowCommandHandler(IBaseRespository<Feedstock> baseEeedstork)
        {
            BaseEeedstork = baseEeedstork;
        }

        public async Task<APIResult<List<FeedbackDTO>>> Handle(FeedstockShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<FeedbackDTO>> ret = new APIResult<List<FeedbackDTO>>();
            try
            {
                var feedstocks = await BaseEeedstork.GetAll();
                var feedbackDTOs = feedstocks.Select(f => new FeedbackDTO
                {
                    FeedstockId = f.FeedstockId,
                    FeedstockName = f.FeedstockName,
                    FeedstockPhoto = f.FeedstockPhoto,
                    IsDeleted = f.IsDeleted
                }).ToList();

                ret.Data = feedbackDTOs;
                ret.Code = ResultCode.查询成功;
                ret.Message = ResultCode.查询成功.ToString();
            }
            catch (Exception ex)
            {
                ret.Code = ResultCode.查询失败;
                ret.Message = ex.Message;
            }

            return ret;
        }
    }
}
